package com.steven.door.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.steven.door.R;
import com.steven.door.base.SelectListener;

import java.util.Objects;


/**
 * 中间dialog的弹出
 */
public class CenterDialog {
    private Activity activity;
    private AlertDialog.Builder builder;
    private View v;
    private int selected;
    public CenterDialog(int layoutId, Activity activity) {
        this.activity = activity;
        this.builder = new AlertDialog.Builder(activity);
        this.v = (LayoutInflater.from(activity)).inflate(layoutId, null);
    }


    private static int dip2px(Context mContext, float dpValue) {
        final float scale = mContext.getResources()
                .getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    //加载动画
    public void showSelect(final SelectListener selectListener, final int position) {
        selected = position;
        TextView confirm=v.findViewById(R.id.tv_confirm);
        TextView cancel=v.findViewById(R.id.tv_cancel);

        RelativeLayout big = v.findViewById(R.id.rel_big);
        RelativeLayout middle = v.findViewById(R.id.rel_middle);
        RelativeLayout small = v.findViewById(R.id.rel_small);
        final ImageView ivBig = v.findViewById(R.id.iv_big);
        final ImageView ivMiddle = v.findViewById(R.id.iv_middle);
        final ImageView ivSmall = v.findViewById(R.id.iv_small);
        switch (position){
            case 3:
                ivBig.setImageResource(R.drawable.select);
                ivMiddle.setImageResource(R.drawable.unselect);
                ivSmall.setImageResource(R.drawable.unselect);
                break;
            case 2:
                ivBig.setImageResource(R.drawable.unselect);
                ivMiddle.setImageResource(R.drawable.select);
                ivSmall.setImageResource(R.drawable.unselect);
                break;
            case 1:
                ivBig.setImageResource(R.drawable.unselect);
                ivMiddle.setImageResource(R.drawable.unselect);
                ivSmall.setImageResource(R.drawable.select);
                break;
        }
        final Dialog dialog = builder.create();
        Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.color.transparent);
        dialog.show();
        //此处设置位置窗体大小
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        Objects.requireNonNull(dialog.getWindow()).setContentView(v);//自定义布局应该在这里添加，要在dialog.show()的后面
        dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
        dialog.setCancelable(true);
        big.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivBig.setImageResource(R.drawable.select);
                ivMiddle.setImageResource(R.drawable.unselect);
                ivSmall.setImageResource(R.drawable.unselect);
                selected=3;
            }
        });
        middle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivBig.setImageResource(R.drawable.unselect);
                ivMiddle.setImageResource(R.drawable.select);
                ivSmall.setImageResource(R.drawable.unselect);
                selected=2;
            }
        });
        small.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivBig.setImageResource(R.drawable.unselect);
                ivMiddle.setImageResource(R.drawable.unselect);
                ivSmall.setImageResource(R.drawable.select);
                selected=1;
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectListener.select(selected);
                dialog.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}
